package projet.volleystat;

import java.util.ArrayList;
import java.util.List;

import projet.volleystat.bdd.VolleyRepository;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class StatSelectPlayersActivity extends Activity {
	protected ListView listv;
	protected MarkPlayersAdapter adapter;
	protected List<Player> players;
	protected VolleyRepository volleyDataBase;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_stat_players_activiry);

		volleyDataBase = new VolleyRepository(getApplicationContext());

		try {
			volleyDataBase.open();
			players = volleyDataBase.getAllPlayer();
			volleyDataBase.close();
		} catch (DatabaseException e) {
			players = new ArrayList<Player>();
			Toast toast = Toast.makeText(this, R.string.dberror,
					Toast.LENGTH_LONG);
			toast.show();
			e.printStackTrace();
		} catch (NullPointerException e) {
			players = new ArrayList<Player>();
			Toast toast = Toast.makeText(this, R.string.error,
					Toast.LENGTH_LONG);
			toast.show();
			e.printStackTrace();
		}

		listv = (ListView) findViewById(R.id.statsplayerlv);

		adapter = new MarkPlayersAdapter(getApplicationContext(),
				R.layout.listitemlineplayerstat, players);

		listv.setAdapter(adapter);

		listv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(getApplication(),
						StatPlayerActivity.class);
				Player player = (Player) parent.getAdapter().getItem(position);
				ArrayList<Integer> players = new ArrayList<Integer>();
				players.add(player.getId());
				intent.putExtra("selectedplayersarrayid", players);
				intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivity(intent);
			}
		});
	}

	@Override
	public void onPause() {
		super.onPause();
		overridePendingTransition(0, 0);
	}
}
